const app = getApp();
var funcs = require('../../static/js/funcs.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    formRes:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    console.log("onload触发")
    var params = decodeURIComponent(options.params);
    var data = JSON.parse(params);
    console.log("跳转后收到的值：")
    console.log(data)
    let groupid = data.groupid
    let openid = app.globalData.openid
    let status = data.status
    if(status=="detail"){
      this.setData({
        getdetailflag: true
      })
      funcs.getKHDetail(openid,groupid).then(res=>{
        if(res.data.succeed){
          console.log(res.data.data.topics)
          this.setData({
            members: res.data.data.members
          })
          wx.hideLoading()
        }
      })
    }else{
      funcs.getGroupDetail(openid,groupid).then(res=>{
        console.log(res.data.data)
        this.setData({
          groupDetail:res.data.data
        })
  
        funcs.getMember(openid,groupid).then(res=>{
          if(res.data.succeed){
            console.log(res.data.data.members)
            this.setData({
              members:res.data.data.members
            })
            wx.hideLoading()
          }
        })
      })
    }
  },

  radioChange(e) {
    let openid = app.globalData.openid
    let data = e.currentTarget.dataset;
    let assess = {
      member_id:Number(data.memberid),
      group_id:Number(this.data.groupDetail.groupId),
      staff_id:openid,
      answer_type:e.detail.value
    }
    console.log("加载的assess为",assess)
    let currlist = this.data.formRes
    console.log("移除前为：",currlist)
    const newArray = currlist.filter(item => item.member_id !== data.memberid); // 移除
    console.log("移除后为：",newArray)
    newArray.push(assess)
    this.setData({
      formRes:newArray
    })
    console.log("当前formRes>",this.data.formRes)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  submitForm(res){
    let openid = app.globalData.openid
    let count = 0
    let countrg = Math.ceil(this.data.members.length * 0.2);
    for(let i=0; i<this.data.formRes.length;i++){
      if(this.data.formRes[i].answer_type=="good"){
        count +=1;
      }
    }
    if(count>=countrg){
      wx.showToast({
        title: "优秀评价应小于"+countrg,
        icon: 'none',
        duration: 3000
      })
      return
    }

    funcs.submitForm2(this.data.formRes).then(res=>{
      if(res.data.succeed){
        wx.showToast({
          title: "提交成功",
          duration: 2000
        })
        wx.reLaunch({
          url: '/pages/assess/assess',
        })
      }else{
        wx.showToast({
          title: "请重新核对",
          icon: 'error',
          duration: 2000
        })
      }
    })
  }
})